/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import Utils.DBConnection;
import Utils.Settings;
import Utils.Utils;
import com.mysql.jdbc.Util;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author user
 */
public class OrderModel {

    DBConnection db = new DBConnection(Settings.getDb(), Settings.getDbLogin(), Settings.getDbPassword());
    String message;

    public int GetOrders_Count() {
        try {
            String query = "SELECT COUNT(*)  AS cnt FROM orders";
            ResultSet rs = db.ExecuteQuery(query);

            if (!rs.next()) {
                return -1;
            }
            return rs.getInt("cnt");
        } catch (Exception ex) {
            return -1;
        } finally {
            //  db.close();
        }
    }

    public ArrayList GetOrders(int offset, int pageSize, String sort, String sortDir) {
        try {
            String querry = "SELECT a.*,b.Username"
                    + " FROM orders a"
                    + " JOIN customers b"
                    + " ON a.CustomerID = b.CustomerID"
                    + " order by " + sort + " " + sortDir + " "
                    + " LIMIT " + offset + ", " + pageSize + " ";

            ResultSet rs = db.ExecuteQuery(querry);
            ArrayList Rows = Utils.resultSetToArrayList(rs);
            return Rows;
        } catch (Exception ex) {
            return null;
        } finally {
            //  db.close();
        }
    }

    public HashMap GetOrder(int orderID) {
        try {
            String querry = "SELECT * from Orders"
                    + " WHERE OrderID=" + orderID;

            ResultSet rs = db.ExecuteQuery(querry);
            ArrayList Rows = Utils.resultSetToArrayList(rs);
            return (HashMap) Rows.get(0);
        } catch (Exception ex) {
            return null;
        } finally {
            //  db.close();
        }
    }

    public int DeleteOrder(int orderID) {
        try {

            String query = "DELETE FROM Orders "
                    + " WHERE OrderID=" + orderID + " ";

            int rows = db.ExecuteUpdate(query);

            if (rows > 0) {
                message = "Comanda a fost stersa cu succes!";
                return 1;
            } else {
                message = "Comanda nu a fost stersa!";
                return -1;
            }

        } catch (Exception ex) {
            return -1;
        } finally {
            //  db.close();
        }
    }

    public int AddOrder(int customerID) {
        try {

            String query = "INSERT INTO orders (`CustomerID`,`Value`,`Complete`,`Date`)"
                    + " VALUES (" + customerID + ",0,0,NOW())";

            int id = db.ExecuteInsert(query);

            if (id != -1) {
                message = "Comanda a fost adaugata!";
                return id;
            } else {
                message = "Comanda nu a fost adaugata!";
                return -1;
            }

        } catch (Exception ex) {
            return -1;
        } finally {
            //  db.close();
        }
    }

    public String GetMessage() {
        return message;
    }
}
